<?php
/**
 * Hello Model for Hello World Component
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/
 * @license		GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class ServersModelServers extends JModel {

    var $_data = null;

    /**
     * Category total
     *
     * @var integer
     */
    var $_total = null;

    /**
     * Pagination object
     *
     * @var object
     */
    var $_pagination = null;

    /**
     * Constructor
     *
     * @since 1.5
     */
    function __construct() {
        parent::__construct();

        global $mainframe, $option;

        // Get the pagination request variables
        $limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', 6, 'int' );
        $limitstart	= JRequest::getVar('limitstart', '0', '', 'int');

        // In case limit has been changed, adjust limitstart accordingly
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }

    /**
     * Method to get dfi_orders item data
     *
     * @access public
     * @return array
     */
    function getData() {
    // Lets load the content if it doesn't already exist
        if (empty($this->lists)) {
            $query = $this->_buildQuery();
            $this->lists = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
            //print_r($this->lists);die;

            return $this->lists;

        }
    //return $this->lists;
    }

    /**
     * Method to get the total number of dfi_order items
     *
     * @access public
     * @return integer
     */
    function getTotal() {
    // Lets load the content if it doesn't already exist
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }

        return $this->_total;
    }

    /**
     * Method to get a pagination object for the dfi_orders
     *
     * @access public
     * @return integer
     */
    function getPagination() {
    // Lets load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }

        return $this->_pagination;
    }

    function _buildQuery() {
    // Get the WHERE and ORDER BY clauses for the query

        $orderby	= $this->_buildContentOrderBy();

        $query = ' SELECT * FROM #__carmanage '
            //. $where
            //. $orderby
        ;

        return $query;
    }

    function _buildContentOrderBy() {
        global $mainframe, $option;

        $orderby 	= '';
        //$orderby 	= ' ';

        return $orderby;
    }

    function getDownload($id) {

		global $mainframe;
		$user		=& JFactory::getUser();
		$aid 		= $user->get('aid', 0);
		$outcome	= array();

		$wheres[]	= " id= ".(int)$id;
		$query = " SELECT file"
				." FROM #__mag_dowload "
				. " WHERE " . implode( " AND ", $wheres )
				;
                
		$filename = $this->_getList( $query, 0, 1 );
                
		// - - - - - - - - - - - - - - -
		if (empty($filename)) {
			$outcome['file'] 		= "PhocaErrorNoDBResult";
			$outcome['directlink']	= 0;
			return $outcome;
		}

		
		$this->_filename 	= $filename[0]->file;
		$this->_directlink 	= 0;
		$filePath			= PhocaDownloadHelper::getPathSet('file');

		if ($this->_filename !='') {

			// Important - you cannot use direct link if you have selected absolute path
			// Absolute Path defined by user
			

			
			$absFile = str_replace('/', DS, JPath::clean($filePath['orig_abs_ds'] . $this->_filename));
			
			if (JFile::exists($absFile)) {                            
				$outcome['file'] 		= $absFile;
				$outcome['directlink']	= $this->_directlink;
				return $outcome;
			} else {

				$outcome['file'] 		= "PhocaErrorNoAbsFile";
				$outcome['directlink']	= 0;
				return $outcome;
			}
		} else {

				$outcome['file'] 		= "PhocaErrorNoDBFile";
				$outcome['directlink']	= 0;
				return $outcome;
		}

	}

}